<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['radio_dj'])) {
    $_SESSION['radio_dj'] = [
        'popularity' => 50, // 电台知名度，范围 0 - 100
        'playlist' => [], // 播放列表
        'listeners' => 100, // 听众数量
        'funds' => 1000, // 资金
        'songs' => [
            'Song A' => 10,
            'Song B' => 15,
            'Song C' => 20
        ]
    ];
}

// 处理添加歌曲到播放列表操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_song'])) {
    $song = $_POST['song'];
    $cost = $_SESSION['radio_dj']['songs'][$song];
    if ($_SESSION['radio_dj']['funds'] >= $cost) {
        $_SESSION['radio_dj']['funds'] -= $cost;
        $_SESSION['radio_dj']['playlist'][] = $song;
        // 简单模拟添加歌曲带来的知名度提升和听众增长
        $_SESSION['radio_dj']['popularity'] += rand(2, 5);
        if ($_SESSION['radio_dj']['popularity'] > 100) {
            $_SESSION['radio_dj']['popularity'] = 100;
        }
        $_SESSION['radio_dj']['listeners'] += rand(10, 30);
        $message = "成功添加 {$song} 到播放列表，花费 {$cost} 元。电台知名度提升，听众增加。";
    } else {
        $message = "资金不足，无法添加 {$song} 到播放列表。";
    }
}

// 处理举办互动活动操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['hold_event'])) {
    $cost = 200;
    if ($_SESSION['radio_dj']['funds'] >= $cost) {
        $_SESSION['radio_dj']['funds'] -= $cost;
        // 举办活动带来的知名度和听众大幅增长
        $_SESSION['radio_dj']['popularity'] += rand(10, 20);
        if ($_SESSION['radio_dj']['popularity'] > 100) {
            $_SESSION['radio_dj']['popularity'] = 100;
        }
        $_SESSION['radio_dj']['listeners'] += rand(50, 100);
        $message = "成功举办互动活动，花费 {$cost} 元。电台知名度和听众数量大幅提升。";
    } else {
        $message = "资金不足，无法举办互动活动。";
    }
}

// 处理播放歌曲操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['play_song'])) {
    if (!empty($_SESSION['radio_dj']['playlist'])) {
        $played_song = array_shift($_SESSION['radio_dj']['playlist']);
        // 播放歌曲带来的小幅度知名度提升和听众增长
        $_SESSION['radio_dj']['popularity'] += rand(1, 3);
        if ($_SESSION['radio_dj']['popularity'] > 100) {
            $_SESSION['radio_dj']['popularity'] = 100;
        }
        $_SESSION['radio_dj']['listeners'] += rand(5, 15);
        $message = "正在播放 {$played_song}，电台知名度和听众数量有所提升。";
    } else {
        $message = "播放列表为空，无法播放歌曲。";
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟电台 DJ 日常工作游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .info-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
    <h1>模拟电台 DJ 日常工作游戏</h1>
    <div class="info-list">
        <p>电台知名度: <?php echo $_SESSION['radio_dj']['popularity']; ?>%</p>
        <p>听众数量: <?php echo $_SESSION['radio_dj']['listeners']; ?></p>
        <p>资金: <?php echo $_SESSION['radio_dj']['funds']; ?> 元</p>
        <p>播放列表: <?php echo implode(', ', $_SESSION['radio_dj']['playlist']) ?: '暂无歌曲'; ?></p>
    </div>

    <?php if (isset($message)): ?>
        <div class="message <?php echo strpos($message, '成功')!== false? 'success' : 'error'; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <label for="song">选择要添加到播放列表的歌曲：</label>
        <select id="song" name="song">
            <?php foreach ($_SESSION['radio_dj']['songs'] as $song => $cost): ?>
                <option value="<?php echo $song; ?>"><?php echo $song; ?> (<?php echo $cost; ?> 元)</option>
            <?php endforeach; ?>
        </select>
        <button type="submit" name="add_song">添加歌曲</button>
    </form>

    <form method="post">
        <button type="submit" name="hold_event">举办互动活动 (200 元)</button>
    </form>

    <form method="post">
        <button type="submit" name="play_song">播放歌曲</button>
    </form>
</body>

</html>